package com.kob.backend.consumer.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Player {
    private Integer id;
    private Integer botId;//-1表示亲自出马，否则ai
    private String botCode;
    private Integer sx;
    private Integer sy;//起点坐标
    //玩家指令序列
    private List<Integer> steps;

    //检查每走一步蛇是否边长
    private boolean check_tail_increasing(int step){//检验当前回合，蛇是否边长
        if(step<=10){
            return true;
        }

        return step%3==1;
    }

    //返回蛇的身体
    public List<Cell> getCells(){
        List<Cell> res = new ArrayList<>();

        int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
        int x=sx,y=sy;
        //回合数
        int step = 0;
        //把起点加进去
        res.add(new Cell(x, y));
        //枚举每一步
        for (int d: steps) {
            x += dx[d];
            y += dy[d];
            res.add(new Cell(x, y));
            if (!check_tail_increasing( ++ step)) {
                res.remove(0);//tail负责增加长度，不增长就删掉tail
            }
        }

        return res;
    }

    public String getStepsString() {
        StringBuilder res = new StringBuilder();
        for (int d: steps) {
            res.append(d);
        }
        return res.toString();
    }
}
